// 专门处理影片相关的接口
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://127.0.0.1:27017/';
// 获取影片列表  location:3000/api/film/list
router.get('/list', function(req, res) {
  var filmId = parseInt(req.query.filmId);
  var results = {};
  MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
    if (err) {
      // 直接返回错误
      console.log('链接数据库失败', err);
      res.json({
        code: 1,
        msg: '网络异常, 请稍候重试'
      })
    } else {
      console.log(filmId)
      var db = client.db('vue-project');
      db.collection('films').find({'filmId':filmId}).toArray(function (err, data) {
        if (err) {
            results.code = -1;
            results.msg = '查询失败';
        } else {
            results.code = 0;
            results.msg = '查询成功';
            results.data = data
        }
        client.close();
        res.json(results);
        })
      }
  })
})
module.exports = router;
